package com.heer.gymnasium.module.fellow.views;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.heer.amap.utils.ToastUtil;
import com.heer.gymnasium.R;
import com.heer.gymnasium.api.ResponseData.ApiResponse;
import com.heer.gymnasium.api.callback.BaseCallback;
import com.heer.gymnasium.base.BaseActivity;
import com.heer.gymnasium.base.BaseRefreshActivity;
import com.heer.gymnasium.constant.Type;
import com.heer.gymnasium.module.evaluate.beans.EvaTagBean;
import com.heer.gymnasium.module.evaluate.beans.MemberItemBean;
import com.heer.gymnasium.module.evaluate.beans.MemberListResponseBean;
import com.heer.gymnasium.module.fellow.beans.FellowMemberBean;
import com.heer.gymnasium.module.login.views.LoginActivity;
import com.heer.gymnasium.util.ImageUtil;
import com.heer.gymnasium.util.StringUtils;
import com.heer.imagepick.DensityUtils;
import com.orangegangsters.github.swipyrefreshlayout.library.SwipyRefreshLayout;
import com.orangegangsters.github.swipyrefreshlayout.library.SwipyRefreshLayoutDirection;
import com.zhihuiyun.library.commonadapter.QuickAdapter;
import com.zhihuiyun.library.commonadapter.QuickTypeAdapter;
import com.zhihuiyun.library.commonadapter.ViewHolder;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import retrofit2.Call;
import retrofit2.Response;

/**
 * Created by yan
 * on 2018/3/21
 */

public class FellowMemberActivity extends BaseActivity {
    private static final int RES_ID_LAYOUT = R.layout.layout_fellow_member;
    private static final int RES_ID_LIST = R.id.layout_fellow_member_lv;

    private static final int RES_ID_ITEM = R.layout.item_fellow_member;
    private static final int RES_ID_ITEM_HEAD = R.id.item_fellow_member_head_iv;
    private static final int RES_ID_ITEM_NAME = R.id.item_fellow_member_name_tv;
    private static final int RES_ID_ITEM_INITIATOR = R.id.item_fellow_member_initiator_tv;
    private static final int RES_ID_ITEM_GV = R.id.item_fellow_member_gv;

    private static final int RES_ID_ITEM_TAG = R.layout.textview;
    private static final int RES_ID_ITEM_TV = R.id.text;

    private Activity $this;

    @BindView(RES_ID_LIST)
    ListView listView;
    private List<FellowMemberBean> data;

    private QuickTypeAdapter<FellowMemberBean> adapter;

    public static void startActivity(Context context, String id) {
        Intent intent = new Intent(context, FellowMemberActivity.class);
        intent.putExtra(EXTRA_ID, id);
        context.startActivity(intent);
    }

    @Override
    protected void init() {
        super.init();
        $this = this;
        setContentView(RES_ID_LAYOUT);
        data = new ArrayList<>();
    }

    @Override
    protected void initView() {
        super.initView();
        titleUtils.setTitleText("活动人员");
        adapter = new QuickTypeAdapter<FellowMemberBean>($this, data, RES_ID_ITEM) {
            @Override
            public void bindData4View(ViewHolder holder, FellowMemberBean data, int type, int pos) {
                ImageUtil.loadUrl($this, data.getAvatar(), R.drawable.ic_default_head, (ImageView) holder.getView(RES_ID_ITEM_HEAD));
                holder.setText(RES_ID_ITEM_NAME, data.getNickname());
                if (data.is_start()) {
                    holder.setVisibility(RES_ID_ITEM_INITIATOR, View.VISIBLE);
                } else {
                    holder.setVisibility(RES_ID_ITEM_INITIATOR, View.GONE);
                }
                List<EvaTagBean> tagList = new ArrayList<EvaTagBean>();
                tagList.addAll(tagToEvaTagBean(true, data.getTag_me()));
                tagList.addAll(tagToEvaTagBean(false, data.getTag_other()));
                ((GridView) holder.getView(RES_ID_ITEM_GV)).setAdapter(new QuickAdapter<EvaTagBean>($this, RES_ID_ITEM_TAG, tagList) {

                    @Override
                    public void bindData4View(ViewHolder holder, EvaTagBean evaTagBean, int pos) {
                        holder.getView(RES_ID_ITEM_TV).setPadding(DensityUtils.dip2px($this, 2), DensityUtils.dip2px($this, 2),
                                DensityUtils.dip2px($this, 2), DensityUtils.dip2px($this, 2));
                        if (evaTagBean.isOwn()) {
                            holder.setTextColor(RES_ID_ITEM_TV, getResources().getColor(R.color.coloryellow));
                            holder.setBg(RES_ID_ITEM_TV, R.drawable.yellow_border);
                        } else {
                            holder.setTextColor(RES_ID_ITEM_TV, getResources().getColor(R.color.white));
                            holder.setBg(RES_ID_ITEM_TV, R.drawable.yellow_button_small);
                        }
                        holder.setText(RES_ID_ITEM_TV, evaTagBean.getName());
                    }
                });

            }
        };
        listView.setAdapter(adapter);
    }

    @Override
    protected void loadData(boolean loadMore) {
        super.loadData(loadMore);
        getFightMemberList();
    }

    //    标签整合
    private List<EvaTagBean> tagToEvaTagBean(boolean isOwn, List<String> data) {
        List<EvaTagBean> beanList = new ArrayList<>();
        if (data != null && data.size() != 0) {
            for (String s : data) {
                EvaTagBean bean = new EvaTagBean();
                if (isOwn) {
                    bean.setOwn(true);
                } else {
                    bean.setOwn(false);
                }
                bean.setName(s);
                beanList.add(bean);
            }
        }
        return beanList;
    }

    private void getFightMemberList() {
        String id = getIntent().getStringExtra(EXTRA_ID);
        if (TextUtils.isEmpty(id)) {
            ToastUtil.show($this, "信息异常");
            finish();
            return;
        }
        mAppHelper.apiService().getFightMemberList(id).enqueue(new BaseCallback<ApiResponse<List<MemberListResponseBean.Item>>>() {
            @Override
            public void onSuccess(Call<ApiResponse<List<MemberListResponseBean.Item>>> call, Response<ApiResponse<List<MemberListResponseBean.Item>>> response) {

                data.clear();
                data.addAll(FellowMemberBean.dataChange(response.body().getData()));
                adapter.notifyDataSetChanged();
            }
        });
    }
}
